<?php
session_start();
require_once('../kelas/record.php');
require_once('../kelas/amankanParam.php');
require_once('../kelas/transaksi.php');
$objParamAman= new amankanParam();
$dtGet = $objParamAman->getMethod();
$dtPost = $objParamAman->postMethod();
if (isset($dtGet['on'])) {
    $on = $dtGet['on'];
    switch ($on) {
        case 'addCart':
        $sk = $objTransaksi->addDetailCart($dtGet['id'], 0, true);
        $al = '';
        if ($sk['success'] == false) {
            $al = 'alert("'.$sk['error_msg'].'");';
        };            
        $echo= "<script type='text/javascript' >".$al."document.location='".BASEURL."index.php?v=cart'</script>";
        echo $echo;
        break;
        case 'hapusCartItem':
        $idGet =$dtGet['id'];
        $sesDetCa = isset($_SESSION['detail_cart'])?$_SESSION['detail_cart']:array();
        $_SESSION['detail_cart']=null;
        foreach ($sesDetCa as $key => $value) {
            if($value['id']==$idGet){
                
            }
            else{
                $_SESSION['detail_cart'][]=$value;   
            }
        }
        echo $objFungsi->redirect(BASEURL.'?v=cart');
        break;
        case 'editCartItems':
        $dtEdit = $dtPost['buy'];
        foreach ($dtEdit as $key => $value){
            $stok = $objTransaksi->cekStok($dtPost['id'][$key]);
            if($stok['stok']>=$value){
                $_SESSION['detail_cart'][$key]['jumlah_buy']=$value;
            }
            else{
                $uns[]=$stok['nama'].' Diminta:'.$value.', Tersedia: '.$stok['stok'];
            }
        };
        $sesDetCa = isset($_SESSION['detail_cart'])?$_SESSION['detail_cart']:array();
        $_SESSION['detail_cart']=null;
            //$objFungsi->var_dump($_SESSION);
        foreach ($sesDetCa as $key => $value) {
            if($value['jumlah_buy']>0){
                $_SESSION['detail_cart'][]=$value;
            }
        }
        if(isset($uns)){
            $tbisa = '\n'.implode('\n',$uns);
            echo "<script type='text/javascript'>alert('Stok tidak mencukupi untuk ".$tbisa."');</script>";
        };
        echo $objFungsi->redirect(BASEURL.'index.php?v=cart');
        break;
        case 'removeCartItem':
        break;
        default:

        break;
    }
} else {

}